package com.four.dailymanage.chamber.service;

import com.four.dailymanage.chamber.dao.ChamberReserveDao;
import com.four.dailymanage.chamber.entity.Chamber;
import com.four.dailymanage.chamber.entity.ChamberReserve;
import com.four.dailymanage.common.bean.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ChamberReserveService {
    @Autowired
    ChamberReserveDao chamberReserveDao;

    //分页查询
    public Page<ChamberReserve> getPageChamberReserve(int pageNum, int pageSize, String cname,String applyDept,String reserveStatus,String startDt,String applyMan){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<ChamberReserve> list=chamberReserveDao.getChamberReserveByCon(start,pageSize,cname,applyDept,reserveStatus,startDt,applyMan);
        //获取总条目数
        int total=chamberReserveDao.countChamberReserve(cname, applyDept, reserveStatus, startDt, applyMan);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<ChamberReserve> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }


    //添加
    public Boolean addReserve(ChamberReserve chamberReserve){
        if (chamberReserve==null){
            return false;
        }
        return chamberReserveDao.addReserve(chamberReserve)>0;
    }

    //检查会议室是否被占用
    public ChamberReserve checkChamberRoom(String cname,String startDt,String endDt){
        return chamberReserveDao.checkRoom(cname, startDt, endDt);
    }

    //更新
    public Boolean update(ChamberReserve chamberReserve){
        if (chamberReserve==null){
            return false;
        }
        chamberReserve.setReserveStatus("同意");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String dataTimeString=sdf.format(new Date());
        if (chamberReserve.getId()<10){
            chamberReserve.setChamberNo(dataTimeString+"00"+chamberReserve.getId());
        }else if (chamberReserve.getId()<100){
            chamberReserve.setChamberNo(dataTimeString+"0"+chamberReserve.getId());
        }else{
            chamberReserve.setChamberNo(dataTimeString+chamberReserve.getId());
        }
        return chamberReserveDao.UpdateStatusById(chamberReserve)>0;
    }

    //更新
    public Boolean reject(ChamberReserve chamberReserve){
        if (chamberReserve==null){
            return false;
        }
        chamberReserve.setReserveStatus("拒绝");
        return chamberReserveDao.UpdateStatusById(chamberReserve)>0;
    }
}
